package com.itfreer.data.report;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

public class ReportUtil {

	/***
	 * 获取where语句
	 * 
	 * @param <T>
	 * @param cls
	 * @param wheremap
	 * @return
	 */
	public static <T> String getWhere(T entity, Map<String, Object> wheremap) {

		String whereString = "";
		Set<String> keys = wheremap.keySet();
		for (String key : keys) {
			String[] fields = key.split(":");
			String field = null;
			String option = null;
			if (fields.length == 2) {
				field = fields[0];
				option = fields[1];
			} else {
				field = key;
				option = "=";
			}

			String whr = getInnerWhere(entity, wheremap, key, field, option);
			if (!whr.equals("")) {
				whereString += whr + " and ";
			}
		}

		if (whereString != "") {

			whereString = whereString.substring(0, whereString.length() - 4);
		}

		return whereString;
	}

	/***
	 * 统计参数查询
	 * 
	 * @param <T>
	 * @param cls
	 * @param wheremap
	 * @param key
	 * @param field
	 * @param option
	 * @return
	 */
	public static <T> String getInnerWhere(T entity, Map<String, Object> wheremap, String key, String field,
			String option) {

		String whereString = "";
		Object value = wheremap.get(key);
		boolean isDateField = false;
		// 处理数据类型，确保Oracle等数据库在日期等数据查询时不出错

		try {
			Field f = entity.getClass().getDeclaredField(field);

			if (f != null && f.getType() == java.util.Date.class) {
				isDateField = true;
			}
			if (option.equals("=")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				if (value.equals("\"null\"")) {
					whereString = field + " is null";
				} else {
					if (isDateField) {
						whereString = " to_date( to_char(" + field + ",'yyyy-mm-dd'),'yyyy-mm-dd') = to_date('" + value
								+ "','yyyy-mm-dd')";
					} else {
						whereString = field + " = '" + value + "'";
					}
				}
			} else if (option.equals(">")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				if (isDateField) {
					whereString = " to_date( to_char(" + field + ",'yyyy-mm-dd'),'yyyy-mm-dd') > to_date('" + value
							+ "','yyyy-mm-dd')";
				} else {
					whereString = field + " > '" + value + "'";
				}
			} else if (option.equals("!=")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				if (isDateField) {
					whereString = " to_date( to_char(" + field + ",'yyyy-mm-dd'),'yyyy-mm-dd') != to_date('" + value
							+ "','yyyy-mm-dd')";
				} else {
					whereString = field + " != '" + value + "'";
				}
			} else if (option.equals(">=")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				if (isDateField) {
					whereString = " to_date( to_char(" + field + ",'yyyy-mm-dd'),'yyyy-mm-dd') >= to_date('" + value
							+ "','yyyy-mm-dd')";
				} else {
					whereString = field + " >= '" + value + "'";
				}
			} else if (option.equals("<")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				if (isDateField) {
					whereString = " to_date( to_char(" + field + ",'yyyy-mm-dd'),'yyyy-mm-dd') < to_date('" + value
							+ "','yyyy-mm-dd')";
				} else {
					whereString = field + " < '" + value + "'";
				}
			} else if (option.equals("<=")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				if (isDateField) {
					whereString = " to_date( to_char(" + field + ",'yyyy-mm-dd'),'yyyy-mm-dd') <= to_date('" + value
							+ "','yyyy-mm-dd')";
				} else {
					whereString = field + " <= '" + value + "'";
				}
			} else if (option.equals("in")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				whereString = field + " in (" + value.toString() + ")";
			} else if (option.equals("nin")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				whereString = field + " not in (" + value.toString() + ")";
			} else if (option.equals("isnull")) {
				whereString = field + " is null";
			} else if (option.equals("isnnull")) {
				whereString = field + " is not null";
			} else if (option.equals("like")) {
				if (value == null || value.equals("") || value.toString() == "null") {
					return whereString;
				}
				whereString = field + " like '%" + value.toString() + "%'";

			} else if (option.equals("%like")) {
				whereString = field + " like '%" + value.toString() + "'";
			} else if (option.equals("like%")) {
				whereString = field + " like '" + value.toString() + "%'";
			} else {
				throw new RuntimeException("不支持的操作符【" + option + "】！");
			}
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}

		return whereString;
	}
}
